//
//  ManagementMainViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/19.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "ManagementMainViewController.h"
#import "MyCollectionViewCell.h"
#import "ContractListViewController.h"
#import "HttpUserAction.h"
#import "MenuItem.h"
#import "ContractMenuViewController.h"
#import "AdvertiserViewController.h"
#import "AgentViewController.h"
#import "ChannelViewController.h"
#import "CustomerListViewController.h"
#import "RemindListViewController.h"
#import "IncomeViewController.h"
#import "HttpHomeAction.h"
#import "CycleScrollView.h"
#import "ImageInfo.h"
#import "UIButton+WebCache.h"
#import "PubInfoViewController.h"
#import "ReportFormViewController.h"

@interface ManagementMainViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,CycleScrollViewDelegate,CycleScrollViewDatasource>{
    UICollectionView *mainCollectionView;
}
@property (nonatomic,strong) NSArray *listArray;
@property (nonatomic,strong) NSArray *headArray;
@property (nonatomic,strong) CycleScrollView *adsCycleScrollView;

@end

@implementation ManagementMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.titleName = @"首页";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initHeadView];
    // Do any additional setup after loading the view.
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //设置headerView的尺寸大小
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 100);
    //该方法也可以设置itemSize
    layout.itemSize =CGSizeMake(110, 150);
    
    //2.初始化collectionView
    float orgin_y = kDevice_Is_iPhoneX?44:0;
    mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, ScreenScale(180)+orgin_y, SCREEN_WIDTH, SCREEN_HEIGHT-TabBarHeight-ScreenScale(180)-orgin_y) collectionViewLayout:layout];
    [self.view addSubview:mainCollectionView];
    mainCollectionView.backgroundColor = [UIColor clearColor];
    
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [mainCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.设置代理
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;
    
//    UIButton *button = [UIButton new];
//    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [button setTitle:@"合同列表" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(contractAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
//    button.sd_layout.leftSpaceToView(self.view, 100).topSpaceToView(self.view, 100).widthIs(100).heightIs(100);
//    
    UserInfo *userInfo = [Utils getUserInfo];
    
    [HttpUserAction getUserMenuWithUserId:userInfo.id block:^(id result, NSError *error) {
        if (!error) {
            self.listArray = result;
            [mainCollectionView reloadData];
        }
    }];
    [self loadPage];
}

-(void)initHeadView{
    float orgin_y = kDevice_Is_iPhoneX?44:0;
    self.adsCycleScrollView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, orgin_y, SCREEN_WIDTH, ScreenScale(180)) withPageControlStyle:PageControlTypeDefault withPageControlFrame:CGRectMake(0,ScreenScale(180)-20,SCREEN_WIDTH, 20) addSubViewToView:self.view];
    //End
    _adsCycleScrollView.pageControl.backgroundColor= [UIColor clearColor];
    _adsCycleScrollView.pageControl.alpha =1.0f;
    _adsCycleScrollView.delegate = self;
    _adsCycleScrollView.datasource =self;
    [self.view addSubview:_adsCycleScrollView];
}

-(void)loadPage{
    [HttpHomeAction getPubInfo:@"header" block:^(id result, NSError *error) {
        if (!error) {
            self.headArray = result;
            [self.adsCycleScrollView reloadData];
        }
    }];

}

-(void)contractAction:(id)sender{
    ContractListViewController *contractListViewController = [[ContractListViewController alloc] init];
    [self.navigationController pushViewController:contractListViewController animated:YES];
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    MenuItem *menuItem = self.listArray[indexPath.row];
    cell.botlabel.text = menuItem.name;
    cell.topImage.image = [UIImage imageNamed:[Utils getIconImageName:menuItem.name]];
//    FuntionInfo *funtionInfo = self.listArray[indexPath.row];
//    cell.botlabel.text =funtionInfo.name;
//    cell.topImage.image = [UIImage imageNamed:[Utils getIconName:funtionInfo.code]];
    //    cell.backgroundColor = [UIColor yellowColor];
    
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/3, 100);
}

//footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}

//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(0, 0);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
    headerView.backgroundColor =[UIColor grayColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:headerView.bounds];
    imageView.image = [UIImage imageNamed:@"mainP"];
    [headerView addSubview:imageView];
    
    
    return headerView;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    MenuItem *menuItem = self.listArray[indexPath.row];
    if ([menuItem.name isEqualToString:@"机位"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TabChange" object:@"1"];
    }else if ([menuItem.name isEqualToString:@"合同"]){
        ContractMenuViewController *contractMenuViewController = [[ContractMenuViewController alloc] init];
        [self.navigationController pushViewController:contractMenuViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"广告商"]){
        AdvertiserViewController *advertiserViewController = [[AdvertiserViewController alloc] init];
        [self.navigationController pushViewController:advertiserViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"代理商"]){
        AgentViewController *agentViewController = [[AgentViewController alloc] init];
        [self.navigationController pushViewController:agentViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"渠道商"]){
        ChannelViewController *channelViewController = [[ChannelViewController alloc] init];
        [self.navigationController pushViewController:channelViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"投资客户"]){
        CustomerListViewController *customerListViewController = [[CustomerListViewController alloc] init];
        [self.navigationController pushViewController:customerListViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"提醒"]){
        RemindListViewController *remindListViewController = [[RemindListViewController alloc] init];
        [self.navigationController pushViewController:remindListViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"收益"]){
        IncomeViewController *incomeViewController = [[IncomeViewController alloc] init];
        [self.navigationController pushViewController:incomeViewController animated:YES];
    }else if([menuItem.name isEqualToString:@"咨询"]){
        PubInfoViewController *pubInfoViewController = [[PubInfoViewController alloc] init];
        [self.navigationController pushViewController:pubInfoViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"报表"]){
        ReportFormViewController *reportFormViewController = [[ReportFormViewController alloc] init];
        [self.navigationController pushViewController:reportFormViewController animated:YES];
    }

}


#pragma mark-CycleScrollViewDelegate
- (NSInteger)numberOfPagesAt:(CycleScrollView *) csView{
    return self.headArray.count;
}

- (UIView *) cycleScrollView:(CycleScrollView *) csView pageAtIndex:(NSInteger)index{
    ImageInfo *imageObject = self.headArray[index];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenScale(180))];
    view.backgroundColor = randomColor;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = view.bounds;
    [view addSubview:button];
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:imageObject.imgUrl] forState:UIControlStateNormal];
    return view;
}

- (void)didClickPage:(CycleScrollView *)csView atIndex:(NSInteger)index{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
